﻿<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:controls="clr-namespace:System.Controls">

  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Common.xaml" />
    <ResourceDictionary Source="RepeatButton.xaml" />
  </ResourceDictionary.MergedDictionaries>

  <Style x:Key="spinEditButtonStyle" TargetType="controls:RepeatButton" BasedOn="{StaticResource {x:Type controls:RepeatButton}}">
    <Setter Property="FontSize" Value="8"/>
    <Setter Property="Height" Value="10"/>
    <Setter Property="Width" Value="21"/>
    <Setter Property="Margin" Value="0"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="Focusable" Value="False"/>
  </Style>

  <ControlTemplate x:Key="controlTemplate" TargetType="{x:Type controls:SpinEdit}">
    <Grid>

      <Grid.ColumnDefinitions>
        <ColumnDefinition x:Name="left_col" Width="Auto" SharedSizeGroup="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SharedSizeGroup}"/>
        <ColumnDefinition />
        <ColumnDefinition x:Name="right_col" Width="Auto"/>
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition x:Name="top_row" Height="Auto"/>
        <RowDefinition />
        <RowDefinition x:Name="bottom_row" Height="Auto"/>
      </Grid.RowDefinitions>

      <controls:TextLabel
        x:Name="label"
        Grid.Column="0"
        Grid.Row="1"
        Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HasContent, Converter={StaticResource b2v}}"
        Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"/>


      <Border 
        x:Name="PART_border"
        Grid.Column="1"
        Grid.Row="1"
        
        CornerRadius="{TemplateBinding CornerRadius}" 
        BorderThickness="{TemplateBinding BorderThickness}" 
        BorderBrush="{TemplateBinding BorderBrush}" 
      
        Background="{TemplateBinding Background}" 
        OpacityMask="{TemplateBinding OpacityMask}" 
      
        MinWidth="{TemplateBinding MinWidth}" 
        MinHeight="{TemplateBinding MinHeight}" 
        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">

        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="Auto"/>
          </Grid.ColumnDefinitions>

          <ScrollViewer 
            x:Name="PART_ContentHost" 
            HorizontalScrollBarVisibility="Disabled" 
            VerticalScrollBarVisibility="Disabled" 
            FontFamily="{TemplateBinding FontFamily}" 
            FontSize="{TemplateBinding FontSize}" 
            FontWeight="{TemplateBinding FontWeight}" 
            Foreground="{TemplateBinding Foreground}" 
            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
            HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
            VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" 
            Padding="{TemplateBinding Padding}" />

          <UniformGrid
            Grid.Column="1"
            Rows="2">

            <controls:RepeatButton 
              VerticalAlignment="Bottom" 
              Content="+" 
              CornerRadius="1,1,0,0" 
              BorderThickness="1,1,1,0"
              Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SpinUpCommand}"
              Style="{StaticResource spinEditButtonStyle}"/>

            <controls:RepeatButton 
              VerticalAlignment="Top" 
              Content="-"
              CornerRadius="0,0,1,1" 
              BorderThickness="1,0,1,1"
              Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SpinDownCommand}"
              Style="{StaticResource spinEditButtonStyle}"/>

          </UniformGrid>


        </Grid>

      </Border>
      
    </Grid>

    <ControlTemplate.Triggers>
      <Trigger Property="ContentDock" Value="Top">
        <Setter Property="Grid.Column" Value="1" TargetName="label"/>
        <Setter Property="Grid.Row" Value="0" TargetName="label"/>
        <Setter Property="SharedSizeGroup" Value="{x:Null}" TargetName="left_col"/>
        <Setter Property="SharedSizeGroup" Value="{x:Null}" TargetName="right_col"/>
      </Trigger>

      <Trigger Property="ContentDock" Value="Bottom">
        <Setter Property="Grid.Column" Value="1" TargetName="label"/>
        <Setter Property="Grid.Row" Value="2" TargetName="label"/>
        <Setter Property="SharedSizeGroup" Value="{x:Null}" TargetName="left_col"/>
        <Setter Property="SharedSizeGroup" Value="{x:Null}" TargetName="right_col"/>
      </Trigger>

      <Trigger Property="ContentDock" Value="Right">
        <Setter Property="Grid.Column" Value="2" TargetName="label"/>
        <Setter Property="Grid.Row" Value="1" TargetName="label"/>
        <Setter Property="SharedSizeGroup" Value="{x:Null}" TargetName="left_col"/>
        <Setter Property="SharedSizeGroup" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SharedSizeGroup}" TargetName="right_col"/>
      </Trigger>

    </ControlTemplate.Triggers>

  </ControlTemplate>


  <Style TargetType="{x:Type controls:SpinEdit}"  BasedOn="{StaticResource {x:Type Control}}">

    <Setter Property="CornerRadius" Value="2" />
    <Setter Property="Padding" Value="3"/>

    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="BorderBrush" Value="DodgerBlue"/>
    <Setter Property="HorizontalContentAlignment" Value="Right" />

    <Setter Property="Foreground" Value="{StaticResource TextBoxForegroundBrush}" />
    <Setter Property="Background" Value="{StaticResource TextBoxBackgroundBrush}" />

    <Setter Property="Template" Value="{StaticResource controlTemplate}" />

    <Style.Triggers>
      <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Opacity" Value="0.75"/>
      </Trigger>

      <Trigger Property="IsFocused" Value="true">
        <Setter Property="Background" Value="{StaticResource TextBoxActiveBackGound}" />
      </Trigger>

    </Style.Triggers>

  </Style>

</ResourceDictionary>